﻿@using Abp.Json
@using Partner.Surround.MultiTenancy.Dto
@model TenantListDto
@section styles{
    <style type="text/css">
        body {
            background-color: #fafafa !important;
        }
    </style>
}

<!--版本列表-->
<div class="layui-fluid">
    <table class="layui-table"
           lay-data="{height: 'full-40', page:false, id:'editionList'}"
           lay-filter="editionList">
        <thead>
            <tr>
                <th lay-data="{type:'radio', fixed: true,width:40}"></th>
                <th lay-data="{field:'displayName'}">版本名称</th>
                <th lay-data="{field:'lastModificationTime',templet:'#lastModificationTime'}">最后修改时间</th>
            </tr>
        </thead>
    </table>
</div>

@await Html.PartialAsync("_LastModificationTime")

@section scripts{
    <script type="text/javascript">
        layui.config({
            base: '/lib/layui-extend-modules/' //静态资源所在路径
        }).use(['jquery', 'form','table'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table;

            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.iframeAuto(index);

            var editionList = @Html.Raw(JsonExtensions.ToJsonString(ViewBag.EditionList, true, false));

            table.reload("editionList", {
                data: editionList,
                limit: editionList.length,
                done: function (res, curr, count) {
                    for (var i = 0; i < res.data.length; i++) {
                        //数据id和要勾选的id相同时radio选中
                        if (res.data[i].id == "@Model.EditionId") {
                            //这里才是真正的有效勾选
                            res.data[i]["LAY_CHECKED"] = 'true';
                            //找到对应数据改变勾选样式，呈现出选中效果
                            var index = res.data[i]['LAY_TABLE_INDEX'];
                            $('.layui-table tr[data-index=' + index + '] input[type="radio"]').prop('checked', true);
                            $('.layui-table tr[data-index=' + index + '] input[type="radio"]').next().addClass('layui-form-radioed');

                            form.val();
                            break;
                        }
                    }
                }
            });

            //监听行改变radio
            table.on("row(editionList)", function (obj) {
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行样式
                obj.tr.find('input[lay-type="layTableRadio"]').prop("checked", true);
                var index = obj.tr.data('index')
                var thisData = table.cache.editionList;

                //重置数据单选属性
                layui.each(thisData, function (i, item) {
                    if (index === i) {
                        item.LAY_CHECKED = true;
                    } else {
                        delete item.LAY_CHECKED;
                    }
                });
                form.render('radio');
            });

            //触发表单提交
            layui.submitForm = function () {
                var checkData = table.checkStatus('editionList').data;
                if (checkData.length == 0) {
                    parent.layer.msg("请选择版本");
                    return;
                }

                if (checkData[0].id == '@Model.EditionId') {
                    parent.layer.msg("版本未变更，无需更换");
                    return;
                }

                var field = {
                    tenantId: '@Model.Id',
                    sourceEditionId: '@Model.EditionId',
                    targetEditionId: checkData[0].id
                };

                var waitIndex = parent.layer.load(2);
                abp.ajax({
                    url: "@Url.Action("MoveTenantToAnotherEdition", "Tenants")",
                    data: JSON.stringify(field),
                    abpHandleError: false
                }).done(function (data) {
                    if (data.code == 200) {
                        data.index = index;
                        parent.layui.currWindow().layui.callback(data);
                    }
                }).fail(function (jqXHR) {
                    parent.layer.msg(jqXHR.message, { icon: 5 });
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

                return false;
            };
        });
    </script>
}

